package financial.sharing.center.system.service.impl;

import com.github.pagehelper.PageInfo;
import financial.sharing.center.common.enums.EnableStatusEnum;
import financial.sharing.center.common.enums.ResponseContext;
import financial.sharing.center.common.vo.Token;
import financial.sharing.center.system.dao.SysUserMapper;
import financial.sharing.center.system.model.entity.SysUser;
import financial.sharing.center.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName SysUserServiceImpl
 * @Description TODO
 * @Author guoyuhang
 * @ @csdn https://blog.csdn.net/qq_40788718
 * @Date 2021/4/22
 * @Version 1.0
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Token getToken(String username, String password) {
        Example example = new Example(SysUser.class);
        example.createCriteria().andEqualTo("userName", username)
                                .andEqualTo("enableStatusValue", EnableStatusEnum.正常.getValue());
        SysUser sysUser = this.getUser(username);
        if (sysUser == null || !password.equals(sysUser.getUserPassword())){
            return new Token(-1, "user", username);
        }
        return new Token(sysUser.getId(), "user", sysUser.getUserName());
    }

    private SysUser getUser(String username){
        Example example = new Example(SysUser.class);
        example.createCriteria().andEqualTo("userName", username)
                                .andEqualTo("enableStatusValue",EnableStatusEnum.正常.getValue());
        SysUser sysUser = this.sysUserMapper.selectOneByExample(example);
        return sysUser;
    }

    private SysUser getUser(Integer id){
        return this.sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer createOneSysUser(String userName, String userPassword) {
        SysUser sysUser = new SysUser(userName, userPassword);
        int i = this.sysUserMapper.insertSelective(sysUser);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.创建失败.getValue();
    }

    @Override
    public Integer compileOneSysUser(SysUser sysUser) {
        int i = this.sysUserMapper.updateByPrimaryKey(sysUser);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.更新失败.getValue();
    }

    @Override
    public PageInfo<SysUser> getAllSysUser(String selectUserName) {
        Example example = new Example(SysUser.class);
        example.createCriteria().andLike("userName", "%"+selectUserName+"%")
                                .andEqualTo("enableStatusValue", EnableStatusEnum.正常.getValue());
        List<SysUser> sysUserList = this.sysUserMapper.selectByExample(example);
        return new PageInfo<>(sysUserList);
    }

    @Override
    public Integer deleteOneSysUser(Integer id) {
        SysUser sysUser = this.getUser(id);
        sysUser.setEnableStatusValue(EnableStatusEnum.弃用.getValue());
        int i = this.sysUserMapper.updateByPrimaryKey(sysUser);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.删除失败.getValue();
    }
}
